package com.education.mymediaplayer.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.education.mymediaplayer.R;
import com.education.mymediaplayer.dao.NewsMediaInfo;
import com.education.mymediaplayer.ui.player.PlayerActivity;
import com.education.mymediaplayer.ui.videoview.VideoPlayActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements MainContract.View, AdapterView.OnItemClickListener {
    @BindView(R.id.listview_video)
    ListView listviewVideo;
    private String url = "http://c.3g.163.com/nc/video/Tlist/T1457069041911/0-10.html";
    private MainContract.Presenter presenter;
    private List<NewsMediaInfo.T1457069041911Bean> mNewsList;
    private NewsMediaAdapter mNewsAdapter;
    private LayoutInflater mInflater;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        new MainPresenter(this);
        presenter.init();
    }

    @Override
    public void setPresenter(MainContract.Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void onInit() {
        //进行初始化
        mInflater = getLayoutInflater();
        mNewsList = new ArrayList<>();
        mNewsAdapter = new NewsMediaAdapter(mInflater, mNewsList);
        listviewVideo.setAdapter(mNewsAdapter);
        listviewVideo.setOnItemClickListener(this);
        presenter.getData(url);
    }

    @Override
    public void onLoadData(List<NewsMediaInfo.T1457069041911Bean> list) {
        mNewsList.addAll(list);
        mNewsAdapter.notifyDataSetChanged();
    }

    @Override
    public void onLoadDataErro() {
        Toast.makeText(MainActivity.this, "连接网络失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        startActivity(new Intent(getApplicationContext(), PlayerActivity.class));
    }
}
